//
//  MainViewController.swift
//  swiftWeibo
//
//  Created by apple on 15/12/8.
//  Copyright © 2015年 张智伟. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {
    //MARK: - 监听方法
    @objc private func clickComposeButton(){
        print("点我了")
    }
    
    //MARK: - 视图声明周期方法
    override func viewDidLoad() {
        super.viewDidLoad()
        addChildViewControllers()
        setcomposeButton()
       }
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        tabBar.bringSubviewToFront(composeButton)
    }
    
//MARK: - 懒加载控件
    private lazy var composeButton: UIButton = UIButton(imageName: "tabbar_compose_icon_add", backImageName: "tabbar_compose_button")


}
//MARK: - 设置界面
extension MainViewController {
    //设置撰写按钮
    private func setcomposeButton(){
        //添加按钮
        tabBar.addSubview(composeButton)
        //设置按钮位置
        let count = childViewControllers.count
        let width = tabBar.bounds.width / CGFloat(count) - 1
        composeButton.frame = CGRectInset(tabBar.bounds, 2 * width, 0)
        //设置监听方法
        composeButton.addTarget(self, action: "clickComposeButton", forControlEvents: .TouchUpInside)
    }
    
    //添加所有子控制器
    private func addChildViewControllers(){
        //设置渲染颜色
        tabBar.tintColor = UIColor.yellowColor()
        addChildViewController(HomeViewController(), title: "首页", imageName: "tabbar_home")
        addChildViewController(DiscoverViewController(), title: "发现", imageName: "tabbar_discover")
        
        addChildViewController(UIViewController())
        
        addChildViewController(MassageViewController(), title: "消息", imageName: "tabbar_message_center")
        addChildViewController(ProfileViewController(), title: "我", imageName: "tabbar_profile")
    }
    
    
    private func addChildViewController(vc: UIViewController,title: String,imageName: String) {
        
        //设置标题
        vc.title = title
        //设置图片
        vc.tabBarItem.image = UIImage(named: imageName)
        //设置导航控制器
        let nav = UINavigationController(rootViewController: vc)
        addChildViewController(nav)
        
    }
}